package com.roc.web.action.terminal;

import java.util.ArrayList;
import java.util.List;

import com.roc.page.BaseSearchCondition;
import com.roc.page.WhereHQLFieldBean;
import com.roc.util.Constance;
import com.roc.util.constant.Role;

public class YsbTermSnStatisticsDetailSearchCondition extends BaseSearchCondition {
	
	
	private static final long serialVersionUID = 1L;

	private String agentId;
	private String roleType;//角色类型

	private String phoneno;// 手机号
	private String assefFlag;// 资产类型
	private String minDate; // 创建开始时间(年月日)
	private String maxDate; // 创建结束时间(年月日)

	@Override
	protected String getOrderPartOfHQL() {
		// TODO Auto-generated method stub
		
		return " order by m.mcht_cd desc ";
	}
	@Override
	protected String getRowCountHQL() {

		StringBuffer sb=new StringBuffer();
		sb.append(" select count(1)");
		sb.append("   from (select distinct i1.asset_flag, ");
		sb.append("                i1.serial_num, ");
		sb.append("                i1.pos_brand, ");
		sb.append("                i1.pos_name, ");
		sb.append("                i1.pos_type ");
		sb.append("           from t_pos_info i1, t_pos_agent p1 ");
		// 查询账号角色非系统管理员 查询当前账号下所有代理商
		if ( !roleType.equals(Role.SYSTEM.getCode()) ) {

			sb.append(	  ", (SELECT TO_CHAR(T.ID) TID "  );
			sb.append(     "         FROM T_OPERATORS T " );
			sb.append("       START WITH ID = " + agentId + " " );
			sb.append("     CONNECT BY PRIOR T.ID = T.SUPRI_ID " );
			sb.append("       ORDER BY T.ID DESC) TT " );
			
		}
		sb.append("          where i1.serial_num = p1.serial_num  and i1.pos_brand = p1.pos_brand ");

		if ( !roleType.equals(Role.SYSTEM.getCode()) ) {
			sb.append("           	and p1.agent_id = TT.TID  " );
		}
		
		sb.append("          ) a, ");
		
		sb.append("        tbl_mcht_inf m, ");
		sb.append("        tbl_term_inf t ");
		sb.append("  where m.mcht_cd = t.mcht_cd ");
		sb.append("    and a.serial_num = t.device_cd ");
		sb.append("    and a.pos_brand = t.tmnl_brand ");
		sb.append("    and instr(t.DEVICE_CD, 'YJ') <= 0 ");
		sb.append("    and t.REC_UPD_TS is not null ");

		return sb.toString();
	}

	@Override
	protected String getSearchHQL() {
		StringBuffer sb=new StringBuffer();
		sb.append(" select m.mcht_cd, m.name_busi, m.contact_phoneno, a.* ");
		sb.append("   from (select distinct i1.asset_flag, ");
		sb.append("                i1.serial_num, ");
		sb.append("                i1.pos_brand, ");
		sb.append("                i1.pos_name, ");
		sb.append("                i1.pos_type ");
		sb.append("           from t_pos_info i1, t_pos_agent p1  ");
		// 查询账号角色非系统管理员 查询当前账号下所有代理商
		if ( !roleType.equals(Role.SYSTEM.getCode()) ) {

			sb.append(	  ", (SELECT TO_CHAR(T.ID) TID "  );
			sb.append(     "         FROM T_OPERATORS T " );
			sb.append("       START WITH ID = " + agentId + " " );
			sb.append("     CONNECT BY PRIOR T.ID = T.SUPRI_ID " );
			sb.append("       ORDER BY T.ID DESC) TT " );
			
		}
		sb.append("          where i1.serial_num = p1.serial_num and i1.pos_brand = p1.pos_brand  and i1.pos_brand = p1.pos_brand ");

		if ( !roleType.equals(Role.SYSTEM.getCode()) ) {
			sb.append("           	and p1.agent_id = TT.TID  " );
		}
		
		sb.append("          ) a, ");
		
		sb.append("        tbl_mcht_inf m, ");
		sb.append("        tbl_term_inf t ");
		sb.append("  where m.mcht_cd = t.mcht_cd ");
		sb.append("    and a.serial_num = t.device_cd ");
		sb.append("    and a.pos_brand = t.tmnl_brand ");
		sb.append("    and instr(t.DEVICE_CD, 'YJ') <= 0 ");
		sb.append("    and t.REC_UPD_TS is not null ");

		return sb.toString(); //y.isBind = 'N'
	}

	@Override
	protected List<WhereHQLFieldBean> getWhereHQLFieldBeanList() {
		List<WhereHQLFieldBean> filedBeanList = new ArrayList<WhereHQLFieldBean>();

		if (Constance.isNotEmpty(phoneno) && !phoneno.equals("")) {
			filedBeanList.add(new WhereHQLFieldBean("m.contact_phoneno", phoneno.trim(), "=", false));
		}
		if (Constance.isNotEmpty(assefFlag) && !assefFlag.equals("-1")) {
			filedBeanList.add(new WhereHQLFieldBean("a.asset_flag", assefFlag.trim(), "=", false));
		}
		if (Constance.isNotEmpty(minDate) && Constance.isNotEmpty(maxDate)) {
			String startDate = minDate.replace("-", "");
			String endDate = maxDate.replace("-", "");
			filedBeanList.add(new WhereHQLFieldBean( "to_char(t.REC_UPD_TS,'yyyyMMdd')", startDate, ">=", false));
			filedBeanList.add(new WhereHQLFieldBean( "to_char(t.REC_UPD_TS,'yyyyMMdd')", endDate, "<=", false));
		}
		return filedBeanList;
	}

	public String getAgentId() {
		return agentId;
	}

	public void setAgentId(String agentId) {
		this.agentId = agentId;
	}

	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}
	public String getPhoneno() {
		return phoneno;
	}
	public void setPhoneno(String phoneno) {
		this.phoneno = phoneno;
	}
	public String getAssefFlag() {
		return assefFlag;
	}
	public void setAssefFlag(String assefFlag) {
		this.assefFlag = assefFlag;
	}
	public String getMinDate() {
		return minDate;
	}
	public void setMinDate(String minDate) {
		this.minDate = minDate;
	}
	public String getMaxDate() {
		return maxDate;
	}
	public void setMaxDate(String maxDate) {
		this.maxDate = maxDate;
	}
	
}
